package com.bct.jxc.dao;
/**
 * 销售退货管理
 */
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.bct.jxc.common.Constants;
import com.bct.jxc.common.ErrorManager;
import com.bct.jxc.common.StringUtil;
import com.bct.jxc.model.SaleInfo;
import com.bct.jxc.model.Saleback;
import com.bct.jxc.model.SalebackInfo;

public class SalebackDao implements BaseDao<Saleback> {
	SqlManager manager = null;

	public SalebackDao() {
		super();
		manager = SqlManager.createInstance();
		manager.connectDB();// 获取数据库连接
	}

	@Override
	public boolean add(Saleback t) {
		// 编写sql语句
		String sql = "insert into t_saleback values(?,?,?,?,?,?,?,?,?,?)";
		// 封装参数
		Object[] params = { t.getId(), t.getAmount(), t.getPrice(), t.getCheckAccept(), t.getCustomerId(),
				t.getSaleTime(), t.getOperator(), t.getSponser(), t.getPayType(), t.getProductId() };
		return manager.executeUpdate(sql, params, Constants.PSTM_TYPE);
	}

	@Override
	public String getId() {
		// sql语句，获得最新的商品编号
		String sql = "select MAX(id) as id from t_saleback";
		ResultSet rs = manager.executeQuery(sql, null, Constants.PSTM_TYPE);
		String id = "SB1001";
		try {
			if (rs != null && rs.next()) {
				String sid = rs.getString("id");// 获取前一次插入的id
				if (sid != null) {
					String s = sid.substring(2);
					id = "SB" + (Integer.parseInt(s) + 1);
				}
			}
		} catch (SQLException e) {
			ErrorManager.printError("SalebackDao getId()", e);
		}
		return id;
	}

	public List<SalebackInfo> findAll(SalebackInfo sInfo){
		String sql = null;
		Object params[] = null;
		List<SalebackInfo> list = null;
		if(sInfo== null) {//查询全部
			sql = "select i.*,g.product_name,p.customer_name from t_saleback i,t_product g,t_customer p "
					+ "where i.product_id=g.id and i.customer_id=p.id";		
		}else {
			if(!StringUtil.isEmpty(sInfo.getId())) {//根据单号查询
				sql="select i.*,g.product_name,p.customer_name from t_saleback i,t_product g,t_customer p " + 
						"  where i.product_id = g.id and i.customer_id = p.id and i.id =?";
				params = new Object[] {sInfo.getId()};
			}else if(!StringUtil.isEmpty(sInfo.getCustomerName())) {//根据客户名字查询
				sql ="select i.*,g.product_name,p.customer_name from t_saleback i,t_product g,t_customer p " + 
						"  where i.product_id = g.id and i.customer_id = p.id and p.customer_name like ?";
				params = new Object[] {"%"+sInfo.getCustomerName()+"%"};
			}else if(!StringUtil.isEmpty(sInfo.getProductName())) {//根据商品名称查询
				sql ="select i.*,g.product_name,p.customer_name from t_saleback i,t_product g,t_customer p " + 
						"  where i.product_id = g.id and i.customer_id = p.id and g.product_name like ?";
				params = new Object[] {"%"+sInfo.getProductName()+"%"};
			}else if(!StringUtil.isEmpty(sInfo.getSponser())) {//根据经手人查询
				sql ="select i.*,g.product_name,p.customer_name from t_saleback i,t_product g,t_customer p " + 
						"  where i.product_id = g.id and i.customer_id = p.id and i.hand_person like ?";
				params = new Object[] {"%"+sInfo.getSponser()+"%"};
			}else if(sInfo.getStartTime()!=null && sInfo.getEndTime()!=null) {//根据日期查询
				sql ="select i.*,g.product_name,p.customer_name from t_saleback i,t_product g,t_customer p " + 
						"  where i.product_id = g.id and i.customer_id = p.id and back_time between ? and ? ";
				params = new Object[] {sInfo.getStartTime(),sInfo.getEndTime()};
			}
		}
		ResultSet rs = manager.executeQuery(sql, params, Constants.PSTM_TYPE);
		if(rs != null) {
			list = new ArrayList<SalebackInfo>();
			try {
				while(rs.next()) {
					SalebackInfo info = new SalebackInfo();
					String id = rs.getString("id");
					int number = Integer.parseInt(rs.getString("amout"));
					float price = Float.parseFloat(rs.getString("price"));
					String checkAccept = rs.getString("check_accept");
					String customerId = rs.getString("customer_id");
					Date saleTime = rs.getDate("back_time");
					String operator = rs.getString("operator");
					String sponsor = rs.getString("hand_person");
					String paytype = rs.getString("method");
					String productId = rs.getString("product_id");
					String productName = rs.getString("product_name");
					String customerName = rs.getString("customer_name");
					info.setId(id);
					info.setAmount(number);
					info.setPrice(price);
					info.setCheckAccept(checkAccept);
					info.setCustomerId(customerId);
					info.setSaleTime(saleTime);
					info.setOperator(operator);
					info.setSponser(sponsor);
					info.setPayType(paytype);
					info.setProductId(productId);
					info.setProductName(productName);
					info.setCustomerName(customerName);
					list.add(info);
				}
			} catch (SQLException e) {
				ErrorManager.printError("InportDao findAll()", e);
			}
		}
		return list;
	}
	@Override
	public List<Saleback> finds(Saleback t) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean update(Saleback t) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean update(String id, Integer spare) {
		// TODO Auto-generated method stub
		return false;
	}
}
